{createAction} = require('redux-actions')

actions =
  getListStart:createAction('TZTZ/GET_LIST_START')
  getZyggListStart:createAction('ZYGG/GET_LIST_START')
  getGdListStart:createAction('GD/GET_LIST_START')

  getListCompleted:createAction('TZ/GET_LIST_COMPLETED')
  getZyggListCompleted:createAction('ZYGG/GET_LIST_COMPLETED')
  getGdListCompleted:createAction('GD/GET_LIST_COMPLETED')

  refreshListStart:createAction('TZ/REFRESH_LIST_START')
  refreshZyggListStart:createAction('ZYGG/REFRESH_LIST_START')
  refreshGdListStart:createAction('GD/REFRESH_LIST_START')

  refreshListCompleted:createAction('TZ/REFRESH_LIST_COMPLETED')
  refreshZyggListCompleted:createAction('ZYGG/REFRESH_LIST_COMPLETED')
  refreshGdListCompleted:createAction('GD/REFRESH_LIST_COMPLETED')

  setMarkRead:createAction('ZZGG/SET_MARK_READ')

  fetchGetList:(pageNum,pageType)->
    (dispatch)->
      switch pageType
        when '1' then dispatch actions.getZyggListStart()
        when '2' then dispatch actions.getListStart()
        when '4' then dispatch actions.getGdListStart()

      fetch("http://202.136.214.173/api/T_CODE_NOTICE?pageType=#{pageType}&pageNum=#{pageNum}",{
          method: 'GET'
          mode: 'cors'
          cache: 'default'
          headers: { 'Content-Type':'application/x-www-form-urlencoded'}
          # body:"pageNum=#{pageNum}&state=1"
        })
      .then (res)->
        res.json()
      .then (json)->
        switch pageType
          when '1'
             dispatch actions.getZyggListCompleted
               list:json
               lastFetchTime:new Date()
          when '2'
            dispatch actions.getListCompleted
              list:json
              lastFetchTime:new Date()
          when '4'
            dispatch actions.getGdListCompleted
              list:json
              lastFetchTime:new Date()
      .catch (err)->
        console.log(err)

  fetchRefreshList:(pageType)->
    (dispatch)->
      switch pageType
        when '1' then dispatch actions.refreshZyggListStart()
        when '2' then dispatch actions.refreshListStart()
        when '4' then dispatch actions.refreshGdListStart()
      fetch("http://202.136.214.173/api/T_CODE_NOTICE?pageNum=1&pageType=#{pageType}",{
          method: 'GET'
          mode: 'cors'
          cache: 'default'
          headers: { 'Content-Type':'application/x-www-form-urlencoded'}
          # body:"pageNum=#{pageNum}&state=1"
        })
      .then (res)->
        res.json()
      .then (json)->
        debugger
        switch pageType
          when '1'
             dispatch actions.refreshZyggListCompleted
               list:json
               lastFetchTime:new Date()
          when '2'
            dispatch actions.refreshListCompleted
              list:json
              lastFetchTime:new Date()
          when '4'
            dispatch actions.refreshGdListCompleted
              list:json
              lastFetchTime:new Date()
      .catch (err)->
        console.log(err)

module.exports = actions
